﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Koala.Pro.Common;
using Koala.Pro.Core.Dto;
using Koala.Pro.Helpers;
using Koala.Pro.Services;
using PMIS.Common;

namespace Koala.Pro.ViewModels
{
    public partial class LoginViewModel : ObservableObject
    {
        private string _username;

        public string Username
        {
            get
            {
                return _username;
            }
            set
            {
                SetProperty(ref _username, value);
                LoginCommand.NotifyCanExecuteChanged();
            }
        }

        private string _password;

        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                SetProperty(ref _password, value);
                LoginCommand.NotifyCanExecuteChanged();
            }
        }

        [ObservableProperty]
        private bool _isLogin;

        [ObservableProperty]
        private bool _isRemberMe;

        public Action LoginCompletionHandler;

        public LoginViewModel()
        {
            GetAccount();
        }

        private void GetAccount()
        {
            IsRemberMe = AppSettingsHelper.IsRemberMe;

            if (!string.IsNullOrWhiteSpace(AppSettingsHelper.UserName))
            {
                Username = EncryptHelper.DesDecrypt(AppSettingsHelper.UserName);
            }
            if (!string.IsNullOrWhiteSpace(AppSettingsHelper.Password))
            {
                Password = EncryptHelper.DesDecrypt(AppSettingsHelper.Password);
            }
        }

        private void SaveAccount()
        {
            AppSettingsHelper.IsRemberMe = IsRemberMe;

            if (IsRemberMe)
            {
                AppSettingsHelper.UserName = EncryptHelper.DesEncrypt(Username);
                AppSettingsHelper.Password = EncryptHelper.DesEncrypt(Password);
            }
            else
            {
                AppSettingsHelper.UserName = string.Empty;
                AppSettingsHelper.Password = string.Empty;
            }
        }

        private bool CanLogin()
        {
            return !string.IsNullOrWhiteSpace(Username) && !string.IsNullOrWhiteSpace(Password);
        }

        [RelayCommand(CanExecute = nameof(CanLogin))]
        private async Task Login()
        {
            if (this.IsLogin) return;

            string ip = NetworkUtil.GetIPAddress();
            string macAddr = string.Concat(HardwareInfoHelper.GetMacAddress());
            string systemType = App.ViewModel.SystemType;

            var dto = new LoginDto();
            dto.Username = this.Username.Trim();
            dto.Password = this.Password ?? "";
            dto.Ip = ip;
            dto.SystemType = systemType;
            dto.MacAddr = macAddr;

            IsLogin = true;

            try
            {
                var loginResult = await BLLFactory<IUserService>.Instance.Login(dto);
                if (loginResult != null && loginResult.UserInfo != null)
                {
                    SaveAccount();
                    await MainModelHelper.SetLoginInfo(loginResult.UserInfo);
                    await App.ViewModel.GetRemoteMenus();
                    LoginCompletionHandler?.Invoke();
                }
                else
                {
                    MessageDxUtil.ShowWarning((loginResult != null) ? loginResult.ErrorMessage : "登录失败,稍后请重试。");
                }
            }
            catch (Exception ex)
            {
                LogTextHelper.Error(ex);
                MessageDxUtil.ShowError(ex.Message);
            }
            finally
            {
                IsLogin = false;
            }
        }
    }
}
